// src/shared/utils/dataLoader.ts

import {User, Product, Category, Order, CartItem, Address, Banner, OrderItem} from '../types';

/**
 * 从JSON文件加载数据的工具函数
 * 注意：这里用import()动态导入，实际使用时需要确保JSON文件在正确位置
 */

/**
 * 加载用户数据
 */
export const loadUsers = async (): Promise<User[]> => {
  try {
    const { default: users } = await import('../../../mock/api/users/list.json');
    return users;
  } catch (error) {
    console.error('Failed to load users data:', error);
    return [];
  }
};

/**
 * 加载商品数据
 */
export const loadProducts = async (): Promise<Product[]> => {
  try {
    const { default: products } = await import('../../../mock/api/products/list.json');
    return products as Product[];
  } catch (error) {
    console.error('Failed to load products data:', error);
    return [];
  }
};

/**
 * 加载热门商品数据
 */
export const loadHotProducts = async (): Promise<Product[]> => {
  try {
    const { default: hotProducts } = await import('../../../mock/api/products/hot.json');
    return hotProducts as Product[];
  } catch (error) {
    console.error('Failed to load hot products data:', error);
    return [];
  }
};

/**
 * 加载商品分类数据
 */
export const loadCategories = async (): Promise<Category[]> => {
  try {
    const { default: categories } = await import('../../../mock/api/products/categories.json');
    return categories;
  } catch (error) {
    console.error('Failed to load categories data:', error);
    return [];
  }
};

/**
 * 加载订单数据
 */
export const loadOrders = async (): Promise<Order[]> => {
  try {
    const { default: orders } = await import('../../../mock/api/orders/list.json');
    return orders as Order[];
  } catch (error) {
    console.error('Failed to load orders data:', error);
    return [];
  }
};

/**
 * 加载订单项数据
 */
export const loadOrderItems = async (): Promise<OrderItem[]> => {
  try {
    const { default: orderItems } = await import('../../../mock/api/orders/items.json');
    return orderItems as OrderItem[];
  } catch (error) {
    console.error('Failed to load order items data:', error);
    return [];
  }
};

/**
 * 加载购物车数据
 */
export const loadCartItems = async (): Promise<CartItem[]> => {
  try {
    const { default: cartItems } = await import('../../../mock/api/cart/items.json');
    return cartItems as CartItem[];
  } catch (error) {
    console.error('Failed to load cart items data:', error);
    return [];
  }
};

/**
 * 加载地址数据
 */
export const loadAddresses = async (): Promise<Address[]> => {
  try {
    const { default: addresses } = await import('../../../mock/api/addresses/list.json');
    return addresses;
  } catch (error) {
    console.error('Failed to load addresses data:', error);
    return [];
  }
};

/**
 * 加载轮播图数据
 */
export const loadBanners = async (): Promise<Banner[]> => {
  try {
    const { default: banners } = await import('../../../mock/api/system/banners.json');
    return banners as Banner[];
  } catch (error) {
    console.error('Failed to load banners data:', error);
    return [];
  }
};

/**
 * 加载所有初始数据
 */
export const loadAllInitialData = async () => {
  const [
    users,
    products,
    hotProducts,
    categories,
    orders,
    orderItems,
    cartItems,
    addresses,
    banners
  ] = await Promise.all([
    loadUsers(),
    loadProducts(),
    loadHotProducts(),
    loadCategories(),
    loadOrders(),
    loadOrderItems(),
    loadCartItems(),
    loadAddresses(),
    loadBanners()
  ]);

  return {
    users,
    products,
    hotProducts,
    categories,
    orders,
    orderItems,
    cartItems,
    addresses,
    banners
  };
};